package cn.yangliu.nacos.oauth2.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.util.HtmlUtils;

@Controller
@SessionAttributes({ "authorizationRequest" })
public class CustomOAuth2Controller {

	/** SCOPES. */
    private static final String SCOPES = "scopes";

	/**
     * Custom custom_approval_page.
     *
     * @param model the model
     * @param request the request
     * @return the access confirmation
     */
	@RequestMapping({ "/oauth/custom_approval_page" })
    public String getAccessConfirmation(Map<String, Object> model,
            HttpServletRequest request) {
        @SuppressWarnings("unchecked")
      /*  Map<String, String> scopes = (Map<String, String>) (model.containsKey(
                SCOPES) ? model.get(SCOPES) : request.getAttribute(SCOPES));*/
        AuthorizationRequest authorizationRequest = (AuthorizationRequest)model.get("authorizationRequest");
        Set<String> scopes = authorizationRequest.getScope();
        List<String> scopeList = new ArrayList<>(scopes);
        model.put("scopeList", scopeList);
        return "oauth_approval";
    }

	/**
     * Custom custom_error_page.
     *
     * @param model the model
     * @param request the request
     * @return the string
     */
	@RequestMapping({ "/oauth/custom_error_page" })
    public String handleError(Map<String, Object> model,
            HttpServletRequest request) {
        Object error = request.getAttribute("error");
        String errorSummary;
        if (error instanceof OAuth2Exception) {
            OAuth2Exception oauthError = (OAuth2Exception) error;
            errorSummary = HtmlUtils.htmlEscape(oauthError.getSummary());
        } else {
            errorSummary = "Unknown error";
        }
        model.put("errorSummary", errorSummary);
        return "oauth_error";
    }

	/**
     * Custom index.
     *
     * @param model the model
     * @param request the request
     * @param response the response
     * @return the string
     */
	@RequestMapping({ "/oauth/index" })
    public String handleIndex(Map<String, Object> model,
            HttpServletRequest request, HttpServletResponse response) {
        AuthenticationException authenticationException = (AuthenticationException) request
                .getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        String errorMsg = "";
        if (authenticationException != null) {
            errorMsg = authenticationException.getMessage();
            model.put("error", errorMsg);
        }
        return "index";
    }

	/**
     * Custom goodbye(Sonar).
     *
     * @param status the status
     */
	@RequestMapping("/oauth/goodbye")
    public void goodbye(SessionStatus status) {
        status.setComplete();
    }

}
